﻿/* 
 *  <copyright file="UseableBase.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using SobrietyEngine.Events;
using SobrietyEngine.Components;
using SobrietyEngine.Common;
using SobrietyEngine.Guids;
using SobrietyEngine.Actor;
using SobrietyEngine.Geometry;

namespace SobrietyEngine.Components.Useable
{
    public class UseableBase: BaseComponent, IUseable, IComponent, IEventListener
    {
        public float UseRadius { get; set; }
        IPosition myPos;

        public UseableBase()
            : base()
        {
        }

        public UseableBase(float useRadius)
            : base()
        {
            UseRadius = useRadius;
        }

        public virtual void RegisterEventListeners()
        {
            Actor.EventRouter.AddEventListener(typeof(Events.ActorActions.Use), this);
        }

        public virtual void UnregisterEventListeners()
        {
            Actor.EventRouter.RemoveEventListener(typeof(Events.ActorActions.Use), this);
        }

        public override bool InitComponent()
        {
            base.InitComponent();
            myPos = Actor.Get<IPosition>();
            if (myPos == null)
                return false; //delay our init    
            RegisterEventListeners();
            return true;
        }

        public override void Remove()
        {
            base.Remove();
            UnregisterEventListeners();
        }

        public void HandleEvent(Event e)
        {
            Events.ActorActions.Use ue = (Events.ActorActions.Use)e.Data;
            TryUse(ue.Who, ue.Where, ue.Range);
        }

        public void TryUse(IActor byWho, Vector2 where, float actorUseRange)
        {
            TryUse(byWho, new Vector3(where, 0), actorUseRange);
        }

        public void TryUse(IActor byWho, Vector3 where, float actorUseRange)
        {
            IPosition actorPos = byWho.Get<IPosition>();
            if (actorPos == null)
            {
                Logging.Logger.Error(this, "Couldnt find actorpos");
                return;
            }

            if (Intersect.PointPoint(myPos.PositionV3, where, UseRadius + actorUseRange))
                ForceUse(byWho);
        }

        public virtual void ForceUse(IActor byWho)
        {
            Logging.Logger.Debug(this, byWho.Guid.Value + " ForceUse()'d base Useable component");
        }
    }
}
